package spring.boot.es.controller;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;

/***
 * https://www.cnblogs.com/cjsblog/p/10232581.html
 *
 * https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-supported-apis.html
 *
 * low -api
 * https://blog.51cto.com/u_13538361/3268459
 */
@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    RestClient restClient ;

    private String index ="book_test";

    @PostMapping("/add")
    public Integer add(@RequestParam String title ) throws Exception{
        Book book = new Book();
        book.setId(System.currentTimeMillis()+"");
        book.setTitle(title);
        book.setAuthor("lilui"+System.currentTimeMillis());
        book.setPrice(10);
        book.setMakeDate(new Date());
        String method = "PUT";
        String endpoint = "/book_test";
        try {

            HttpEntity entity = new NStringEntity(JSONObject.toJSONString(book), ContentType.APPLICATION_JSON);
            Request request  = new Request(method, endpoint);
            request.setEntity(entity);
            Response response= restClient.performRequest(request);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }catch (Exception e) {
            e.printStackTrace();
        }


        //删
      /*  DeleteRequest deleteRequest = new DeleteRequest("指定index", "指定type", "指定ID");
        highLevelClient.delete(deleteRequest,null);
*/
        //改, source 里对象创建方式可以是JSON字符串，或者Map，或者XContentBuilder 对象
       /* UpdateRequest updateRequest = new UpdateRequest("指定index", "指定type", "指定ID").doc(builder);
        highLevelClient.update(updateRequest,null);*/
        //查
       /* GetRequest getRequest = new GetRequest("指定index", "指定type", "指定ID");
        highLevelClient.get(getRequest,null);*/


        //Bulk批量操作
        /*BulkRequest request = new BulkRequest();
        request.add(new IndexRequest("指定index", "指定type", "指定ID_1").source(XContentType.JSON,"field", "foo"));
        request.add(new DeleteRequest("指定index", "指定type", "指定ID_2"));
        request.add(new UpdateRequest("指定index", "指定type", "指定ID_3") .doc(XContentType.JSON,"other", "test"));
        BulkResponse bulkResponse = highLevelClient.bulk(request);
        for (BulkItemResponse bulkItemResponse : bulkResponse) {
            if (bulkItemResponse.isFailed()) {
                BulkItemResponse.Failure failure = bulkItemResponse.getFailure();
                continue;
            }
            DocWriteResponse itemResponse = bulkItemResponse.getResponse();
            if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.INDEX
                    || bulkItemResponse.getOpType() == DocWriteRequest.OpType.CREATE) {
                IndexResponse indexResponse = (IndexResponse) itemResponse;
            } else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.UPDATE) {
                UpdateResponse updateResponse = (UpdateResponse) itemResponse;
            } else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.DELETE) {
                DeleteResponse deleteResponse = (DeleteResponse) itemResponse;
            }
        }*/


        // SearchRequest高级查询
       /* SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder builder = new SearchSourceBuilder();
        DayOfWeek searchSourceBuilder;
        searchSourceBuilder.query(xxxQuery);
        searchRequest.source(builder);*/
        return 1;
    }
}
